package com.quality.framework.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.quality.common.utils.SecurityUtils;
import com.quality.framework.web.service.TokenService;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author hjh
 * 自动填充字段
 */
@Component
public class AutoFillFieldsHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String username = SecurityUtils.getUsername();
        Date date = new Date();
        this.strictInsertFill(metaObject,"createBy", String.class, username);
        this.strictInsertFill(metaObject,"updateBy", String.class, username);
        this.strictInsertFill(metaObject,"createTime", Date.class, date);
        this.strictInsertFill(metaObject,"updateTime",Date.class, date);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String username = SecurityUtils.getUsername();
        Date date = new Date();
        this.strictInsertFill(metaObject,"updateBy", String.class, username);
        this.strictInsertFill(metaObject,"updateTime",Date.class, date);
    }
}